package com.xiaoyun.vo;

import com.google.common.base.Optional;
import com.xiaoyun.constant.FeedbackType;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static com.google.common.base.Preconditions.checkNotNull;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class Feedback {
    //用户id
    private Long userId;
    //评论类型
    private String type;
    //passTemplate RowKey 如果是 app 类型的评论， 则没有
    private String templateId;
    //评论内容
    private String comment;

    public boolean validate() {
        FeedbackType feedbackType = this.getIfPresent(
                FeedbackType.class, this.type.toUpperCase()
        ).orNull();
        return !(null == feedbackType || null == comment);
    }

    public static <T extends Enum<T>> Optional<T> getIfPresent(Class<T> enumClass, String value) {
        checkNotNull(enumClass);
        checkNotNull(value);
        try {
            return Optional.of(Enum.valueOf(enumClass, value));
        } catch (IllegalArgumentException iae) {
            return Optional.absent();
        }
    }
}
